<?php

namespace sil29\VitrineBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use sil29\VitrineBundle\Entity\User;
use sil29\VitrineBundle\Form\UserType;

/**
 * User controller.
 */
class UserController extends Controller {
	
	/**
	 * Lists all User entities.
	 */
	public function indexAction() {
		$em = $this->getDoctrine ()->getManager ();
		
		$entities = $em->getRepository ( 'sil29VitrineBundle:User' )->findAll ();
		
		return $this->render ( 'sil29VitrineBundle:User:index.html.twig', array (
				'entities' => $entities 
		) );
	}
	/**
	 * Creates a new User entity.
	 */
	public function createAction(Request $request) {
		$entity = new User ();
		$form = $this->createCreateForm ( $entity );
		$form->handleRequest ( $request );
		
		if ($form->isValid ()) {
			$em = $this->getDoctrine ()->getManager ();
			$em->persist ( $entity );
			$em->flush ();
			
			// Envoie du mail de bienvenue
			$message = \Swift_Message::newInstance ()->setSubject ( "Bienvenue " . $entity->getLogin () )->setFrom ( 'admin@g-ladalle.com' )->setTo ( $entity->getEmail () )->setBody ( $this->renderView ( 'sil29VitrineBundle:Mail:mailbienvenue.txt.twig', array (
					'login' => $entity->getLogin (),
					'motDePasse' => $entity->getMotDePasse () 
			) ) );
			$this->get ( 'mailer' )->send ( $message );
			
			// Mise en session de l'utilisateur fraichement crée
			$session = $this->getRequest()->getSession();
			$session->set('idUser', $entity->getId());
			
			
			return $this->redirect ( $this->generateUrl ( 'user_show', array (
					'id' => $entity->getId () 
			) ) );
		}
		
		return $this->render ( 'sil29VitrineBundle:User:new.html.twig', array (
				'entity' => $entity,
				'form' => $form->createView () 
		) );
	}
	
	/**
	 * Creates a form to create a User entity.
	 *
	 * @param User $entity
	 *        	The entity
	 *        	
	 * @return \Symfony\Component\Form\Form The form
	 */
	private function createCreateForm(User $entity) {
		$form = $this->createForm ( new UserType (), $entity, array (
				'action' => $this->generateUrl ( 'user_create' ),
				'method' => 'POST' 
		) );
		
		$form->add ( 'submit', 'submit', array (
				'label' => 'Create' 
		) );
		
		return $form;
	}
	
	/**
	 * Displays a form to create a new User entity.
	 */
	public function newAction() {
		$entity = new User ();
		$form = $this->createCreateForm ( $entity );
		
		return $this->render ( 'sil29VitrineBundle:User:new.html.twig', array (
				'entity' => $entity,
				'form' => $form->createView () 
		) );
	}
	
	/**
	 * Finds and displays a User entity.
	 */
	public function showAction($id) {
		$em = $this->getDoctrine ()->getManager ();
		
		$entity = $em->getRepository ( 'sil29VitrineBundle:User' )->find ( $id );
		
		if (! $entity) {
			throw $this->createNotFoundException ( 'Unable to find User entity.' );
		}
		
		$deleteForm = $this->createDeleteForm ( $id );
		
		return $this->render ( 'sil29VitrineBundle:User:show.html.twig', array (
				'entity' => $entity,
				'delete_form' => $deleteForm->createView () 
		) );
	}
	
	/**
	 * Displays a form to edit an existing User entity.
	 */
	public function editAction($id) {
		$em = $this->getDoctrine ()->getManager ();
		
		$entity = $em->getRepository ( 'sil29VitrineBundle:User' )->find ( $id );
		
		if (! $entity) {
			throw $this->createNotFoundException ( 'Unable to find User entity.' );
		}
		
		$editForm = $this->createEditForm ( $entity );
		$deleteForm = $this->createDeleteForm ( $id );
		
		return $this->render ( 'sil29VitrineBundle:User:edit.html.twig', array (
				'entity' => $entity,
				'edit_form' => $editForm->createView (),
				'delete_form' => $deleteForm->createView () 
		) );
	}
	
	/**
	 * Creates a form to edit a User entity.
	 *
	 * @param User $entity
	 *        	The entity
	 *        	
	 * @return \Symfony\Component\Form\Form The form
	 */
	private function createEditForm(User $entity) {
		$form = $this->createForm ( new UserType (), $entity, array (
				'action' => $this->generateUrl ( 'user_update', array (
						'id' => $entity->getId () 
				) ),
				'method' => 'PUT' 
		) );
		
		$form->add ( 'submit', 'submit', array (
				'label' => 'Update' 
		) );
		
		return $form;
	}
	/**
	 * Edits an existing User entity.
	 */
	public function updateAction(Request $request, $id) {
		$em = $this->getDoctrine ()->getManager ();
		
		$entity = $em->getRepository ( 'sil29VitrineBundle:User' )->find ( $id );
		
		if (! $entity) {
			throw $this->createNotFoundException ( 'Unable to find User entity.' );
		}
		
		$deleteForm = $this->createDeleteForm ( $id );
		$editForm = $this->createEditForm ( $entity );
		$editForm->handleRequest ( $request );
		
		if ($editForm->isValid ()) {
			$em->flush ();
			
			return $this->redirect ( $this->generateUrl ( 'user_edit', array (
					'id' => $id 
			) ) );
		}
		
		return $this->render ( 'sil29VitrineBundle:User:edit.html.twig', array (
				'entity' => $entity,
				'edit_form' => $editForm->createView (),
				'delete_form' => $deleteForm->createView () 
		) );
	}
	/**
	 * Deletes a User entity.
	 */
	public function deleteAction(Request $request, $id) {
		$form = $this->createDeleteForm ( $id );
		$form->handleRequest ( $request );
		
		if ($form->isValid ()) {
			$em = $this->getDoctrine ()->getManager ();
			$entity = $em->getRepository ( 'sil29VitrineBundle:User' )->find ( $id );
			
			if (! $entity) {
				throw $this->createNotFoundException ( 'Unable to find User entity.' );
			}
			
			$em->remove ( $entity );
			$em->flush ();
		}
		
		return $this->redirect ( $this->generateUrl ( 'user' ) );
	}
	
	/**
	 * Creates a form to delete a User entity by id.
	 *
	 * @param mixed $id
	 *        	The entity id
	 *        	
	 * @return \Symfony\Component\Form\Form The form
	 */
	private function createDeleteForm($id) {
		return $this->createFormBuilder ()->setAction ( $this->generateUrl ( 'user_delete', array (
				'id' => $id 
		) ) )->setMethod ( 'DELETE' )->add ( 'submit', 'submit', array (
				'label' => 'Delete' 
		) )->getForm ();
	}
}
